<template>
    <div class="staff-container">
        <el-card>
            <el-card class="header">
                <el-form ref="form" :model="form" label-width="80px">
                    <el-form-item label="员工姓名">
                        <el-input v-model="form.employeeName" placeholder="请输入姓名" class="query"></el-input>
                    </el-form-item>
                    <el-button type="primary" @click="getList">查询</el-button>
                </el-form>
            </el-card>
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column prop="employeeCode" label="工号">
                </el-table-column>
                <el-table-column prop="employeeName" label="姓名">
                </el-table-column>
                <el-table-column prop="employeeGender" label="性别">
                </el-table-column>
                <el-table-column prop="employeeAge" label="年龄">
                </el-table-column>
                <el-table-column prop="entryTime" label="入职时间">
                </el-table-column>
                <el-table-column align="center" label="职务" width="180">
                    <template slot-scope="scope">
                        {{ getStaffName(scope.row.staff) }}
                    </template>
                </el-table-column>
                <el-table-column prop="description" label="备注信息" width="180">
                </el-table-column>
                <el-table-column label="操作" width="150">
                    <template slot-scope="scope">
                        <el-button @click="handleClick(scope.row)" type="primary" plain size="mini">编辑</el-button>
                        <el-button @click="handleClick(scope.row)" type="danger" plain size="mini">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination :current-page="currentPage" :page-sizes="[10, 30, 60, 90]" :page-size="pageSize"
                    :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    layout="total, sizes, prev, pager, next, jumper">
                </el-pagination>
            </div>
        </el-card>
        <el-dialog title="添加员工信息" :visible.sync="dialogVisible" :before-close="handleClose">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="工号" prop="employee_account">
                    <el-input v-model="ruleForm.employee_account" disabled placeholder="工号由系统生成"></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="employee_name">
                    <el-input v-model="ruleForm.employee_name"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="employee_age">
                    <el-input v-model="ruleForm.employee_age"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="employee_gender">
                    <el-radio-group v-model="ruleForm.employee_gender">
                        <el-radio label="男"></el-radio>
                        <el-radio label="女"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="职务" prop="employee_staff">
                    <el-input v-model="ruleForm.employee_staff"></el-input>
                </el-form-item>
                <el-form-item label="备注信息" prop="employee_message">
                    <el-input v-model="ruleForm.employee_message"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
            <!-- <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
            </span> -->
        </el-dialog>
    </div>
</template>
<script>
import request from '@/utils/request';
export default {
    data() {
        return {
            form: {
                employeeName: null
            },
            dialogVisible: false,
            tableData: [],
            currentPage: 1,
            pageSize: 10,
            total: 0,
            ruleForm: {
                employee_account: '',
                employee_name: '',
                employee_age: '',
                employee_gender: '男',
                employee_staff: '',
                employee_message: ''
            },
            rules: {
                employee_name: [
                    { required: true, message: '请输入员工姓名', trigger: 'blur' }
                ],
                employee_staff: [
                    { required: true, message: '请输入职务', trigger: 'blur' }
                ],
                employee_age: [
                    { required: true, message: '年龄不能为空' },
                    { type: 'number', message: '年龄必须为数字值' }
                ],
            }
        }
    },
    methods: {
        handleSizeChange(val) {
            this.pageSize = val
            this.getList()
        },
        handleCurrentChange(val) {
            this.currentPage = val
            this.getList()
        },
        handleClick(val) {
            this.getList()
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done()
                })
                .catch(_ => { })
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    alert('submit!');
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        getList() {
            request({
                url: '/gym-admin/employee/list',
                method: 'get',
                params: {
                    employeeName: this.form.employeeName,
                    pageNo: this.currentPage,
                    pageSize: this.pageSize
                }
            }).then(res => {
                const data = res.data
                this.tableData = data.records
                this.currentPage = data.current
                this.total = data.total
                console.log(res);
            })
        },
        getStaffName(staff) {
            switch (staff) { 
                case "01":
                return "健身教练"
                case "02":
                return "保洁员"
            }
        }
    },
    created() {
        this.getList()
    }
}
</script>

<style scoped lang='scss'>
.staff-container {
    .header {
        .query {
            width: 300px;
            padding-right: 10px;
        }
    }

    .pagination {
        padding-top: 10px;
    }
}
</style>
